<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'data'.DS.'DAOPoi.php');
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');

class JDAOPoi implements DAOPoi{
	
	public function __construct(){
		
	}
	
	private function multiObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		$rows = $db->loadObjectList();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		return $rows;
	}
	
	public function getRoutePOIs($id){
		$rows=$this->multiObjectQuery("SELECT * FROM #__sr_pois WHERE routeID=".$id);
		return DTOFactory::getInstance()->getDTOPOIsFromObject($rows);
	}
	
	public function savePOI($poi){
		$row =& JTable::getInstance('pois', 'Table');
		$row->routeID=$poi->getRoute();
		$row->subCategoryID=$poi->getSubCategory();
		$row->name=$poi->getName();
		$row->description=$poi->getDescription();
		$row->location=$poi->getRawLocation();
		if (!$row->store()){
			return false;
		}
		return $row->id;
	}
	
	public function updatePOI($poi){
		$row =& JTable::getInstance('pois', 'Table');
		$row->id=$poi->getId();
		$row->routeID=$poi->getRoute();
		$row->subCategoryID=$poi->getSubCategory();
		$row->name=$poi->getName();
		$row->description=$poi->getDescription();
		$row->location=$poi->getRawLocation();
		if (!$row->store()){
			return false;
		}
		return $row->id;
	}
	
	public function deletePOI($id){
		$row =& JTable::getInstance('pois', 'Table');
		return $row->delete($id);
	}
	
}

?>